[Asp. Net MVC4] Verify user login implementation instance

  • 2021-09-04 23:52:27
  • OfStack

Recently, we are going to do a micro blog imitating sina. It happens that I am studying mvc recently, so I want to use mvc technology to realize this project.

Since it is Weibo, you should know that there must be users logging in without thinking about it, but it is different from the conventional asp.NET logging in. The following is my research results in 1 afternoon +1 evening ~ ~ ~

First of all, build the database and tables, which goes without saying.

Let's talk about the main structure under 1

Controller:

HomeController This is the controller for the home page

LoginController This is the login controller

Class:

CDBTemplate. cs This is the database data corresponding class, which describes the structure of the database

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

First, the return function of HomeController controller


public ActionResult Index(){...} 

Add before:


[Authorize(Roles = "admins")] 

There it goes:


[Authorize(Roles = "admins")] 
public ActionResult Index() 
{ 
  ... 
} 

This statement means to add a permission authentication to this, allowing access only to users whose user role is admins

Then add to the web. config file:


<authentication mode="Forms"> 
   <forms loginUrl="~/Login" timeout="2880" /> 
</authentication> 

These mean to add user authentication to the whole website, and point to the login interface which is login controller

CDBTemplate. 1 class in the cs file:


public class LogOnModel 
  { 
    [Required] 
    [Display(Name = " User name ")] 
    public string UserName { get; set; } 
 
 
    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = " Password ")] 
    public string Password { get; set; } 
 
 
    [Display(Name = " Automatic login next time ")] 
    public bool RememberMe { get; set; } 
  } 

Then add a view Index. cshtml to the default return function of the LoginController controller, and add the following code to the page:


@model Weibo.Models.LogOnModel //LogOnModel  Yes CDBTemplate.cs In the file 1 Category  
@using (Html.BeginForm("Login","Login",FormMethod.Post)) { 
  @Html.TextBoxFor(m => m.UserName) 
        @Html.ValidationMessageFor(m => m.UserName, " Please enter the user name! ", new {style="color: #f00" }) 
@Html.PasswordFor(m => m.Password) 
        @Html.ValidationMessageFor(m => m.Password," Please enter the password! ",new {style="color: #f00" }) 
@Html.CheckBoxFor(m => m.RememberMe) 
        @Html.LabelFor(m => m.RememberMe) 
@Html.ActionLink(" Forgot password ", "forgotpwd", null, new {@class="rt",target="_blank" }) 
<input type="submit" value=" Log on to Weibo " /> 
}

In the above code, the first parameter of Html. BeginForm ("Login", "Login", FormMethod. Post) method means the name of the method specifying the controller to be called, the second parameter means the name of the controller, and the third parameter means what method to submit the form to the server. Here, for the sake of safety, we choose to submit the form in post mode.

Then add such a method to the LoginController controller:


[HttpPost, ActionName("Login")] 
    public void Login(FormCollection collection) 
    { 
      object obj = SqlHelper.ExecuteScalar("select UserId from CDBUsers where UserName=@uname and Password=@pwd", 
         new SqlParameter("@uname", collection[0]), 
         new SqlParameter("@pwd", Weibo.Models.Myencrypt.myencrypt(collection[1]))); 
 
 
      if (obj != null) 
      { 
        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 
          1, 
          collection[0], 
          DateTime.Now, 
          DateTime.Now.AddMinutes(30), 
          false, 
          "admins" 
          ); 
        string encryptedTicket = FormsAuthentication.Encrypt(authTicket); 
        System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); 
        System.Web.HttpContext.Current.Response.Cookies.Add(authCookie); 
      } 
 
 
      Response.Redirect("~/"); 
    } 

Okay, it's done ~ ~ ~ ~


Related articles: